﻿using System;
using System.Collections.Generic;
using System.Text;

namespace IllustratedCsharp7_ch9_OperatorOverloading
{
    class chapt9_15OperatorOverloading
    {
       
      static void Main()
        {
            LimitedInt li1 = new LimitedInt();
            LimitedInt li2 = new LimitedInt();
            LimitedInt li3 = new LimitedInt();
            li1.TheValue = 10; li2.TheValue = 26;
            Console.WriteLine($"li1: {li1.TheValue}, li2:{li2.TheValue}");
            li3 = -li1;
            Console.WriteLine($"-{li1.TheValue} = {li3.TheValue}");

            li3 = li2 - li1;
            Console.WriteLine($"{li2.TheValue} - {li1.TheValue} = {li3.TheValue}");

            li3 = li1 - li2;
            Console.WriteLine($"{li1.TheValue} - {li2.TheValue} = {li3.TheValue}");

            li3 = li1 + 10;
            Console.WriteLine($"{li1.TheValue} + 10 = {li3.TheValue}");

        }
    }

    class LimitedInt
    {
        const int MaxValue = 100;
        const int MinValue = 0;

        public static LimitedInt operator -(LimitedInt x)
        {
            //在这个奇怪的类中，取一个值的的负数等于0
            LimitedInt li = new LimitedInt();
            li.TheValue = 0;
            return li;
        }

        public static LimitedInt operator -(LimitedInt x,LimitedInt y)
        {           
            LimitedInt li = new LimitedInt();
            li.TheValue = x.TheValue - y.TheValue;
            return li;
        }

        public static LimitedInt operator +(LimitedInt x, double y)
        {
            LimitedInt li = new LimitedInt();
            li.TheValue = x.TheValue + (int)y;
            return li;
        }
        private int mTheValue = 0;
        public int TheValue
        {
            get { return mTheValue; }
            set
            {
                if (value < MinValue)
                    mTheValue = 0;
                else
                    mTheValue = value > MaxValue
                                ? MaxValue
                                : value;
            }
        }
    }
}
